<template>
	<view class="thepatient">
		<view class="content">
			<view class="uni-form-item uni-column">
				<!--  @input="fpNumInput"  -->
				<view class="title">姓名</view>
				<input class="uni-input" v-model="formPatient.name" name="input" placeholder="请填写姓名" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">年龄</view>
				<input class="uni-input" type="number" v-model="formPatient.age" name="input" placeholder="请填写年龄" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">性别</view>

				<!-- <input class="uni-input" v-model="formPatient.sex" name="input" placeholder="请填写性别" /> -->
				<view class="uni-list">
					<radio-group @change="radioChange">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="index">
							<view class="item">
								<radio :value="item.value" :checked="index + 1 === current" />
								{{ item.name }}
							</view>
							<!-- <view></view> -->
						</label>
					</radio-group>
				</view>
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">身份证号</view>
				<input class="uni-input" type="idcard" v-model="formPatient.idcard" name="input"
					placeholder="请填写身份证号" />
			</view>
			<view class="uni-form-item uni-column">
				<!--  @click="add" -->
				<view class="title">手机号</view>
				<input class="uni-input" type="number" v-model="formPatient.phone" name="input" placeholder="请填写手机号" />
			</view>

			<view class="uni-column">
				<text>住址</text>
				<!-- placeholder-style="color:#F76260" -->
				<textarea v-model="formPatient.address" placeholder="请填写住址"></textarea>
			</view>
		</view>

		<view class="bottom_to">
			<view class="add_to" @click="addtoThePatient">完成添加</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from "vuex";
	import {
		postPatient,
		thePatientDetails
	} from '@/api/appointment.js';
	export default {
		computed: {
			...mapGetters(['routeError']),
		},
		data() {
			return {
				current: null,

				tile: '',
				formPatient: {
					name: '',
					age: '',
					sex: null,
					idcard: '',
					phone: '',
					address: '',
					id: null
				},
				items: [{
						value: '1',
						name: '男'
					},
					{
						value: '2',
						name: '女'
					}
				],
				isAddNow: false
			};
		},
		onLoad(options) {
			if (this.routeError) {
				this.$store.commit('GO_ERROR');
				return
			}
			this.formPatient.id = options.id;

			if (this.formPatient.id != null) {
				this.getPatient();
			}
		},
		methods: {
			radioChange: function(evt) {
				this.formPatient.sex = evt.detail.value;
				//    for (let i = 0; i < this.items.length; i++) {
				// if (this.items[i].value === evt.detail.value) {
				// 		this.current = this.items[i].value;
				// 		break;
				// }
				//      }
			},
			getPatient: function() {
				thePatientDetails({
						id: this.formPatient.id
					})
					.then(res => {
						this.formPatient = res.data;
						this.current = res.data.sex;
					})
					.catch(err => {
						that.$util.Tips({
							title: error
						});
					});
			},
			addtoThePatient() {
				if (this.isAddNow) {
					return
				}
				this.isAddNow = true
				var that = this;
				postPatient(that.formPatient)
					.then(res => {
						uni.hideLoading()
						return that.$util.Tips({
							title: res.message,
							icon: 'success'
						}, {
							tab: 3
						});
					})
					.catch(err => {
						setTimeout(function() {
							that.isAddNow = false
						}, 2000);
						return that.$util.Tips({
							title: err
						});
					});
			},
			fpNumInput(e) {
				const o = e.target;
				const inputRule = /^(0+)|[^\d]+/g; //修改inputRule 的值
				this.$nextTick(function() {
					this.formPatient.name = o.value.replace(inputRule, '');
				});
			}
		}
	};
</script>

<style lang="scss">
	.thepatient {
		padding: 0upx;

		.content {
			background-color: #ffffff;
			padding: 20upx;

			.uni-form-item {
				display: flex;
				padding-left: 50upx;
				height: 90upx;
				line-height: 90upx;
				border-bottom: 4upx solid #f7f7f7;

				.uni-input {
					flex: 1;
					margin-top: 24upx;
				}

				.uni-list {
					radio-group {
						display: flex;

						label {
							margin-right: 20upx;
						}
					}
				}

				.title {
					font-size: 34upx;
					margin-right: 50upx;
					min-width: 20%;
				}
			}

			.uni-column {
				padding-left: 30upx;

				text {
					color: #9c9c9c;
				}
			}
		}

		.bottom_to {
			position: fixed;
			bottom: 0px;
			width: 100%;
			height: 120upx;
			background-color: $yy-main-col;
			color: #FFFFFF;
			font-size: 36upx;
			text-align: center;
			line-height: 120upx;
		}
	}
</style>